      implicit undefined (a-z)
      common /xxx/col,b,n,pm,start,dcta,dta,won,pplim,tdlim,ppart
      integer i,j,dcta,dctb,dta(50),dtb(50),iabs,jacobi,mod,col,idec
      integer tdlim,ppart(1600,40),lmlim
      logical islm
      integer b,n,pm,del,lm,lmmod,lmres,start,limit,won(2),pplim
      open(1,file='appc')
10    read(5,100,end=99) b,n,pm,del,lm,lmmod,lmres,start,limit,lmlim
100   format(10i7)
      write(6,100) b,n,pm,del,lm,lmmod,lmres,start,limit,lmlim
      tdlim=100000
      pplim=1+lmlim/3
      won(1)=2 
      won(2)=1
23000 if(.not.(n .le. lmlim))goto 23002
      islm = (lm .eq. 1) .and. (mod(n,lmmod) .eq. lmres)
      if(n .gt. limit .and. .not. islm)then
      goto 23001
      endif
      dcta=0
      dctb=0
      if(and(n,1) .eq. 1 .and. n .gt. 1)then
      dcta=1
      dta(1)=1
      endif
      do23007 i=2,n
      j=n/i
      if(j .lt. i)then
      goto 23008
      endif
      if(n .ne. i*j)then
      goto 23007
      endif
      if(and(i,1) .eq. 1)then
      dctb=dctb+1
      dtb(dctb)=j
      endif
      if(j .le. i)then
      goto 23008
      endif
      if(and(j,1) .eq. 1)then
      dcta=dcta+1
      dta(dcta)=i
      endif
23007 continue
23008 continue
      if(dctb .gt. 0)then
      do23021 i=1,dctb
      dcta=dcta+1
      dta(dcta)=dtb(dctb+1-i)
23021 continue
23022 continue
      endif
      call putdec(6,n,5)
      call putc(' ') 
      call putc(' ')
      col=7
      if(islm)then
      dctb=0
      do23025 i=1,dcta
      j=jacobi(b,n/dta(i))
      if(j .eq. 0)then
      dctb=dctb+1
      if(dctb .eq. 1)then
      call putc('(')
      else
      call putc(',')
      endif
      call putdec(6,dta(i),1)
      if(mod(dta(i),lmmod) .eq. lmres)then
      call putc('L')
      call putc(',')
      call putdec(6,dta(i),1)
      call putc('M')
      endif
      endif
      dta(i) = j*dta(i)
23025 continue
23026 continue
      call putc(')')
      call putc(' ')
      call putc('L')
      call putc('.')
      call putc('M')
      call putc(char(10))
      do23033 i=1,5
      call putc(' ')
23033 continue
23034 continue
      call putc('L')
      call putc(' ')
      j=0
      do23035 i=1,dcta
      if(dta(i) .ne. 0)then
      j=j+1
      if(j .eq. 1)then
      call putc('(')
      else
      call putc(',')
      endif
      col=col+idec(iabs(dta(i)))+2
      if(dta(i) .gt. 0)then
      call putc('L')
      else
      call putc('M')
      endif
      endif
23035 continue
23036 continue
      if(j .gt. 0)then
      call putc(')')
      col=col+1
      endif
      col=col+1
      call putc(' ')
      call proc(1)
      do23045 i=1,5
      call putc(' ')
23045 continue
23046 continue
      call putc('M')
      call putc(' ')
      col=7
      j=0
      do23047 i=1,dcta
      if(dta(i) .ne. 0)then
      j=j+1
      if(j .eq. 1)then
      call putc('(')
      else
      call putc(',')
      endif
      col=col+idec(iabs(dta(i)))+2
      if(dta(i) .gt. 0)then
      call putc('M')
      else
      call putc('L')
      endif
      endif
23047 continue
23048 continue
      if(j .gt. 0)then
      call putc(')')
      col=col+1
      endif
      col=col+1
      call putc(' ')
      call proc(-1)
      else
      if(dcta .gt. 0)then
      do23059 i=1,dcta
      if(i .eq. 1)then
      call putc('(')
      else
      call putc(',')
      endif
      col=col+idec(dta(i))+1
23059 continue
23060 continue
      call putc(')')
      col=col+1
      endif
      call putc(' ')
      col=col+1
      call proc(0)
      endif
23001 n=n+del
      goto 23000
23002 continue
      goto 10
99    stop
      end
      integer function idec(n)
      implicit undefined (a-z)
      character ch(79)
      integer itoc,i,n,nd
      nd=itoc(n,ch,79)
      do23063 i=1,nd
      call putc(ch(i))
23063 continue
23064 continue
      idec=nd
      return
      end
      subroutine apcdec(k)
      integer k,i,itoc,j
      character lildig(10)
      i=itoc(k,lildig,10)
      do23065 j=i,3
      call fputc(1,' ')
23065 continue
23066 continue
      do23067 j=1,i
      call fputc(1,lildig(j))
23067 continue
23068 continue
      return
      end
      subroutine proc(m)
      implicit undefined (a-z)
      common /xxx/col,b,n,pm,start,dcta,dta,won,pplim,tdlim,ppart
      integer m,col,b,n,pm,start,dcta,dta(50),won(2),pplim,ppart(1600,40)
      integer t,i,j,k,ppt(80),tmp(80),quot(80),rem(80)
      integer div1,gcd1,tdlim,idec,mpitoc
      character bigdig(400)
      if(m .eq. 0)then
      call mpow(b,n,ppt)
      if(pm .eq. 1)then
      call add(ppt,won,ppt)
      else
      call sub(ppt,won,ppt)
      endif
      if(dcta .gt. 0)then
      do23075 i=1,dcta
      k=ppart(dta(i),1)
      do23077 j=1,k
      tmp(j)=ppart(dta(i),j)
23077 continue
23078 continue
      call div(ppt,tmp,quot,rem)
      if(rem(1).gt.2 .or. rem(2).gt.0)then
      write(6,101) b,n,m,i,dta(i)
101   format('error dividing cofactor',5i12)
      stop
      endif
      call mpcopy(quot,ppt)
23075 continue
23076 continue
      endif
      if(n .le. pplim)then
      do23083 j=1,ppt(1)
      ppart(n,j)=ppt(j)
23083 continue
23084 continue
      endif
      else
      if(m .eq. 1)then
      call aurif(b,n,m,ppt)
      do23087 i=1,dcta
      t=iabs(dta(i))
      if(t .eq. 0)then
      goto 23087
      endif
      if(dta(i) .lt. 0)then
      t=t+800
      endif
      k=ppart(t,1)
      do23093 j=1,k
      tmp(j)=ppart(t,j)
23093 continue
23094 continue
      call div(ppt,tmp,quot,rem)
      if(rem(1).gt.2 .or. rem(2).gt.0)then
      write(6,101) b,n,m,i,dta(i)
      stop
      endif
      call mpcopy(quot,ppt)
23087 continue
23088 continue
      if(n .le. pplim)then
      do23099 j=1,ppt(1)
      ppart(n,j)=ppt(j)
23099 continue
23100 continue
      endif
      else
      call aurif(b,n,m,ppt)
      do23101 i=1,dcta
      t=iabs(dta(i))
      if(t .eq. 0)then
      goto 23101
      endif
      if(dta(i) .gt. 0)then
      t=t+800
      endif
      k=ppart(t,1)
      do23107 j=1,k
      tmp(j)=ppart(t,j)
23107 continue
23108 continue
      call div(ppt,tmp,quot,rem)
      if(rem(1).gt.2 .or. rem(2).gt.0)then
      write(6,101) b,n,m,i,dta(i)
      stop
      endif
      call mpcopy(quot,ppt)
23101 continue
23102 continue
      if(n .le. pplim)then
      do23113 j=1,ppt(1)
      ppart(n+800,j)=ppt(j)
23113 continue
23114 continue
      endif
      endif
      endif
      t=gcd1(div1(ppt,n,tmp),n)
      if(t .gt. 1)then
      col=col+idec(t)+2
      call putc('*') 
      call putc('.')
      j=div1(ppt,t,ppt)
      endif
      if(n .lt. start)then
      if(n .lt. 40)then
      call mptdec(6,ppt,1)
      else
      call putc('o')
      call putc('m')
      call putc('i')
      call putc('t')
      endif
      else
      if(n.eq.1)then
23123 if(and(ppt(2),1).eq.0)then
      col=col+idec(2)+1
      call putc('.')
      ppt(2)=ppt(2)/2
      goto 23123
      endif
23124 continue
      endif
      k=n+n
      t=k+1
23125 if(.not.(t.lt.tdlim))goto 23127
23128 if(div1(ppt,t,tmp).eq.0)then
      col=col+idec(t)+1
      call putc('.')
      call mpcopy(tmp,ppt)
      goto 23128
      endif
23129 continue
      if(ppt(1).eq.2 .and. ppt(2).eq.1)then
      goto 23127
      endif
23126 t=t+k
      goto 23125
23127 continue
      do23132 i=col,73
      call putc(' ')
23132 continue
23133 continue
      call putc('C')
      k=mpitoc(ppt,bigdig,400)
      i=idec(k)
      call apcdec(k)
      call apcdec(b)
      call fputc(1,',')
      call apcdec(n)
      if(m.eq.0)then
      if(pm.eq.1)then
      call fputc(1,'+')
      else
      call fputc(1,'-')
      endif
      else
      if(m.eq.1)then
      call fputc(1,'L')
      else
      call fputc(1,'M')
      endif
      endif
      call fputc(1,' ')
      call fputc(1,' ')
      do23140 j=1,k
      if(j .gt. 1 .and. mod(j,100) .eq. 1)then
      call fputc(1,char(10))
      call apcdec(k)
      do23144 i=1,12
      call fputc(1,' ')
23144 continue
23145 continue
      endif
      call fputc(1,bigdig(j))
23140 continue
23141 continue
      call fputc(1,char(10))
      endif
      call putc(char(10))
      return
      end
      subroutine mpow(b,n,pow)
      implicit undefined (a-z)
      integer b,n,pow(80)
      integer e,i,j,bits(30),tmp(80)
      pow(1)=2
      pow(2)=b
      e=n
      i=0
      do23146 j=1,30
      i=i+1
      bits(i)=and(e,1)
      e=e/2
      if(e.eq.0)then
      goto 23147
      endif
23146 continue
23147 continue
      i=i-1
23150 if(.not.(i.gt.0))goto 23152
      call mul(pow,pow,tmp)
      if(bits(i) .ne. 0)then
      call mul1(tmp,b,tmp)
      endif
      call mpcopy(tmp,pow)
23151 i=i-1
      goto 23150
23152 continue
      return
      end
      subroutine aurif(b,n,m,ppt)
      integer b,n,m,ppt(80),w(80),x(80),y(80),z(80),h,k
      h=n/b
      k=(h+1)/2
      I23155=(b)
      goto 23155
23157 continue
      call mpow(3,h,x)
      call mpow(3,k,y)
      x(2)=x(2)+1
      if(m.eq.1)then
      call sub(x,y,ppt)
      else
      call add(x,y,ppt)
      endif
      goto 23156
23160 continue
      call mpow(b,h,z)
      call mpcopy(z,x)
      x(2)=x(2)+1
      call mul(x,x,y)
      call add(y,z,y)
      call mpow(b,k,z)
      call mul(x,z,w)
      if(m.eq.1)then
      call sub(y,w,ppt)
      else
      call add(y,w,ppt)
      endif
      goto 23156
23163 continue
      call mpow(7,h,w)
      w(2)=w(2)+1
      call mul(w,w,x)
      call mul(w,x,ppt)
      w(2)=w(2)-1
      call sub(x,w,x)
      call mpow(7,k,y)
      call mul(y,x,z)
      if(m.eq.1)then
      call sub(ppt,z,ppt)
      else
      call add(ppt,z,ppt)
      endif
      goto 23156
23166 continue
      call mpow(10,h,z)
      call mul1(z,5,x)
      call mul(z,z,y)
      call mul(y,y,w)
      call add(y,z,z)
      call add(z,z,z)
      call mul1(y,7,y)
      call add(x,y,y)
      call add(w,y,y)
      z(2)=z(2)+1
      y(2)=y(2)+1
      call mpow(10,3*h,w)
      call add(w,z,z)
      call mul1(w,5,w)
      call add(w,y,y)
      call mpow(10,k,w)
      call mul(w,z,x)
      if(m.eq.1)then
      call sub(y,x,ppt)
      else
      call add(y,x,ppt)
      endif
      goto 23156
23169 continue
      call mpow(11,h,z)
      call mul(z,z,y)
      call mul(y,y,x)
      call add(x,z,x)
      call mul1(x,5,z)
      call sub(x,y,x)
      call sub(z,y,z)
      x(2)=x(2)+1
      z(2)=z(2)+1
      call mpow(11,3*h,w)
      call sub(z,w,z)
      call add(x,w,x)
      call mpow(11,5*h,w)
      call add(z,w,z)
      call mpow(11,k,w)
      call mul(w,x,y)
      if(m.eq.1)then
      call sub(z,y,ppt)
      else
      call add(z,y,ppt)
      endif
      goto 23156
23172 continue
      h=n/3
      k=(h+1)/2
      call mpow(2,h,x)
      call mpow(3,k,y)
      call mul(x,y,z)
      call mpow(12,h,y)
      y(2)=y(2)+1
      if(m.eq.1)then
      call sub(y,z,ppt)
      else
      call add(y,z,ppt)
      endif
      goto 23156
23175 continue
      write(6,100) b
100   format('aurif not defined for b =',i10)
      stop
23155 continue
      I23155=I23155-2
      if (I23155.lt.1.or.I23155.gt.10)goto 23175
      goto (23157,23175,23160,23160,23163,23175,23175,23166,23169,23172),I23155
23156 continue
      return
      end
      integer function gcd1(u,v)
      implicit undefined (a-z)
      integer u,v,u3,v3,t,mod
      u3=u
      v3=v
23176 if(v3.ne.0)then
      t=mod(u3,v3)
      u3=v3
      v3=t
      goto 23176
      endif
23177 continue
      gcd1=u3
      return
      end
